/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cip.esymons.tramite.bc;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.cip.esymons.general.be.Solicitante;
import org.cip.esymons.tramite.be.Plantilla;
import org.cip.esymons.tramite.be.TipoDocumento;

/**
 *
 * @author FreddyHIQ
 */
@Stateless
public class PlantillaFacade implements PlantillaFacadeLocal {

    @PersistenceContext
    private EntityManager em;

    public void create(Plantilla plantilla) {
        em.persist(plantilla);
    }

    public void edit(Plantilla plantilla) {
        em.merge(plantilla);
    }

    public void remove(Plantilla plantilla) {
        em.remove(em.merge(plantilla));
    }

    public Plantilla find(Object id) {
        return em.find(Plantilla.class, id);
    }

    public List<Plantilla> listarPublicoXDocumento(TipoDocumento doc) {
        try{
        return em.createQuery("select object(o) from Plantilla as o where o.fkTdoCodigo=?1 and o.plaPublico=1").setParameter(1, doc).getResultList();
        }catch(Exception e){
            return new ArrayList<Plantilla>();
        }
    }

    public List<Plantilla> listarXSolicitante(Solicitante sol, TipoDocumento doc) {
        try{
        Query q = null;
        StringBuilder sb = new StringBuilder();
        sb.append(" select object(o) from Plantilla as o where o.fkSolCodigo=?1");
        if(doc!=null){
            sb.append(" and o.fkTdoCodigo=?2");
        }
        q = em.createQuery(sb.toString());
        q.setParameter(1, sol);
        if(doc!=null){
            q.setParameter(2, doc);
        }

        return q.getResultList();
        }catch(Exception e){
            return new ArrayList<Plantilla>();
        }
    }

    public List<Plantilla> findAll() {
        return em.createQuery("select object(o) from Plantilla as o").getResultList();
    }
}
